﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace slCommunicator
{
    public partial class SessionForm : Form
    {
        public static SessionForm currentSessionForm = null;
        public static bool bDrawWave = Properties.Settings.Default.doShowWaveform;

        private NAudio.Gui.WaveformPainter wfp;
        public SessionForm()
        {
            InitializeComponent();
            currentSessionForm = this;

            if (bDrawWave)
            {
                wfp = new NAudio.Gui.WaveformPainter();
                wfp.Dock = DockStyle.Fill;
                bottomPanel.Controls.Add(wfp);
            }
        }

        public static void DrawWave(byte[] audio)
        {
            try
            {
                if (!bDrawWave)
                    return;
                if (currentSessionForm == null)
                    return;
                if (currentSessionForm.wfp == null)
                    return;

                currentSessionForm.BeginInvoke(new MethodInvoker(delegate
                    {
                        for (int index = 0; index < audio.Length - 1; index += 64)
                        {
                            short sample = (short)((audio[index + 1] << 8) | (audio[index]));
                            float sample16 = sample / 16384f;
                            currentSessionForm.wfp.AddMax(sample16);
                        }
                    }));
            }
            catch (Exception e) { Console.WriteLine("DrawWave " + e.Message); }
        }

        public static void Create(ContactInformation him)
        {
            if (currentSessionForm != null)
                throw new Exception(localization.session_multisessionNotSupported);

            new Task(() => CreateAndShow(him)).Start();
        }

        public static void InvokeClose()
        {
            if (currentSessionForm == null)
                return;
            if (currentSessionForm.IsDisposed)
                return;

            if (currentSessionForm.InvokeRequired)
            {
                currentSessionForm.Invoke(new MethodInvoker(InvokeClose));
            }
            else
            {
                currentSessionForm.Close();
            }
        }

        private static void CreateAndShow(ContactInformation him)
        {
            if (Application.VisualStyleState == System.Windows.Forms.VisualStyles.VisualStyleState.NoneEnabled)
                Application.EnableVisualStyles();

            SessionForm sf = new SessionForm();
            sf.callerPicture.ImageLocation = him.PictureProfileUrl;
            sf.callerDisplayLabel.Text = him.DisplayName;
            currentSessionForm = sf;
            Application.Run(sf);
        }

        private void SessionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            currentSessionForm = null;
            if (Session.currentSession != null)
                Session.currentSession.Terminate();
        }
    }
}
